<<<<<<< HEAD
/* * @Author: Zhengxuan Ye * @Date: 2025-05-06 21:28:34 * @Last Modified by: Zhengxuan Ye * @Last
Modified time: 2025-05-06 21:28:34 */
=======
/*
 * @Author: Zhengxuan Ye
 * @Date: 2025-05-06 21:28:34
 * @Last Modified by:   Zhengxuan Ye
 * @Last Modified time: 2025-05-06 21:28:34
 */
>>>>>>> ba93636 (前端建设完成)

<template>
  <div class="login-container">
    <div class="form-container">
      <h1>用户登录</h1>
      <p v-if="error" class="error-message">{{ error }}</p>

      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="username"
            required
            placeholder="请输入用户名"
            autocomplete="username"
          />
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="password"
            required
            placeholder="请输入密码"
            autocomplete="current-password"
          />
        </div>

        <div class="form-group captcha-group">
          <label for="captcha">验证码</label>
          <div class="captcha-container">
            <input
              type="text"
              id="captcha"
              v-model="captchaInput"
              required
              placeholder="请输入验证码"
              maxlength="4"
              autocomplete="off"
            />
            <div class="captcha-box" @click="refreshCaptcha">
              <span v-if="captchaLoading" class="loading-text">
                <span class="dot"></span>
                <span class="dot"></span>
                <span class="dot"></span>
              </span>
              <span v-else class="captcha-text">{{ captchaCode }}</span>
            </div>
          </div>
          <div class="captcha-refresh" @click="refreshCaptcha">看不清？点击刷新</div>
        </div>

        <div class="form-actions">
          <button type="submit" class="btn-login" :disabled="loading">
            {{ loading ? '登录中...' : '登录' }}
          </button>
          <router-link to="/register" class="register-link"> 没有账号？立即注册 </router-link>
        </div>
      </form>
    </div>

    <div class="login-image">
      <div class="image-container">
        <img :src="loginImage" alt="中国非物质文化遗产" class="heritage-image" />
        <div class="image-source">素材来源于中国非物质文化遗产网</div>
      </div>
      <div class="image-text">
        <h2>传承中华文明 弘扬传统文化</h2>
        <p>登录后体验更多非物质文化遗产精彩内容</p>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../../stores/user'
import loginImage from '../../assets/loginAndRegister.png'
<<<<<<< HEAD
import axios from 'axios'
=======
>>>>>>> ba93636 (前端建设完成)

export default {
  name: 'LoginView',
  setup() {
    const userStore = useUserStore()
    const router = useRouter()
    const route = useRoute()

    const username = ref('')
    const password = ref('')
    const captchaInput = ref('')
    const captchaCode = ref('')
    const captchaId = ref('')
    const captchaLoading = ref(false)
    const loading = computed(() => userStore.loading)
    const error = computed(() => userStore.error)

    // 获取验证码
    const fetchCaptcha = async () => {
      try {
        captchaLoading.value = true
        // 使用正确的API URL路径
        const response = await axios.get('http://localhost:3000/api/user/captcha')
        captchaCode.value = response.data.captcha
        captchaId.value = response.data.captchaId
        console.log('获取到验证码:', captchaCode.value) // 调试日志
      } catch (error) {
        console.error('获取验证码失败:', error)
        userStore.error = '获取验证码失败，请刷新页面重试'
      } finally {
        captchaLoading.value = false
      }
    }

    // 刷新验证码
    const refreshCaptcha = () => {
      fetchCaptcha()
    }

    // 如果用户已登录，重定向到首页或来源页面
    onMounted(() => {
      if (userStore.isLoggedIn) {
        const redirectPath = route.query.redirect || '/'
        router.push(redirectPath)
      } else {
        // 获取验证码
        fetchCaptcha()
      }
    })

    // 处理登录
    const handleLogin = async () => {
      if (loading.value) return

      if (!username.value || !password.value || !captchaInput.value) {
        userStore.error = '请填写所有必填字段'
        return
      }

      const success = await userStore.login({
        username: username.value,
        password: password.value,
        captchaId: captchaId.value,
        captcha: captchaInput.value,
      })

      if (success) {
        // 登录成功，重定向到首页或来源页面
        const redirectPath = route.query.redirect || '/'
        router.push(redirectPath)
      } else {
        // 登录失败，刷新验证码
        fetchCaptcha()
        captchaInput.value = ''
      }
    }

    return {
      username,
      password,
      captchaInput,
      captchaCode,
      captchaLoading,
      loading,
      error,
      handleLogin,
<<<<<<< HEAD
      refreshCaptcha,
=======
>>>>>>> ba93636 (前端建设完成)
      loginImage,
    }
  },
}
</script>

<style scoped>
.login-container {
  display: flex;
  min-height: calc(100vh - 180px);
  background-color: var(--light-color);
}

.form-container {
  flex: 1;
  padding: 3rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  max-width: 500px;
}

.login-image {
  flex: 1;
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 2rem;
}

.image-container {
  width: 100%;
  height: 300px;
  position: relative;
  margin-bottom: 2rem;
  border-radius: var(--border-radius);
  overflow: hidden;
}

.heritage-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-source {
  position: absolute;
  right: 10px;
  bottom: 10px;
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.8rem;
}

h1 {
  font-size: 2rem;
  margin-bottom: 1.5rem;
  color: var(--primary-color);
  font-family: 'ZCOOL QingKe HuangYou', sans-serif;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

label {
  font-weight: bold;
}

input {
  padding: 0.8rem 1rem;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius);
  font-size: 1rem;
  transition: var(--transition);
}

input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(197, 48, 48, 0.2);
}

.captcha-group label {
  margin-bottom: 8px;
}

.captcha-container {
  display: flex;
  gap: 10px;
}

.captcha-container input {
  flex: 1;
}

.captcha-box {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 120px;
  height: 44px;
  background-color: #f0f0f0;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius);
  font-size: 1.8rem;
  font-weight: bold;
  letter-spacing: 5px;
  cursor: pointer;
  user-select: none;
  color: #333;
  position: relative;
  overflow: hidden;
  padding: 0 10px;
}

.captcha-text {
  display: inline-block;
  width: 100%;
  text-align: center;
  transform: translateY(0);
  font-family: 'Courier New', monospace;
}

.captcha-refresh {
  font-size: 0.8rem;
  color: var(--primary-color);
  cursor: pointer;
  margin-top: 5px;
  text-align: right;
}

.captcha-refresh:hover {
  text-decoration: underline;
}

.form-actions {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 1rem;
}

.btn-login {
  background-color: var(--primary-color);
  color: white;
  padding: 0.8rem 1rem;
  border: none;
  border-radius: var(--border-radius);
  font-size: 1rem;
  cursor: pointer;
  transition: var(--transition);
}

.btn-login:hover {
  background-color: #b42c2c;
}

.btn-login:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.register-link {
  text-align: center;
  color: var(--primary-color);
  text-decoration: none;
  font-size: 0.9rem;
}

.register-link:hover {
  text-decoration: underline;
}

.error-message {
  background-color: #fef2f2;
  color: var(--danger-color);
  padding: 0.8rem;
  border-radius: var(--border-radius);
  margin-bottom: 1rem;
}

@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }

  .form-container {
    padding: 2rem 1rem;
    max-width: 100%;
  }

  .login-image {
    order: -1;
    padding: 1rem;
  }

  .image-container {
    height: 200px;
    margin-bottom: 1rem;
  }

  .captcha-container {
    flex-direction: column;
    gap: 10px;
  }

  .captcha-box {
    width: 100%;
  }
}

.loading-text {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  gap: 6px;
}

.dot {
  width: 8px;
  height: 8px;
  background-color: var(--primary-color);
  border-radius: 50%;
  display: inline-block;
  animation: bounce 1.4s infinite ease-in-out both;
}

.dot:nth-child(1) {
  animation-delay: -0.32s;
}

.dot:nth-child(2) {
  animation-delay: -0.16s;
}

@keyframes bounce {
  0%,
  80%,
  100% {
    transform: scale(0);
  }
  40% {
    transform: scale(1);
  }
}
</style>
